<?php

class action_install
{
	var $install;
	
	 function deformatNVP($nvpstr)
		{
	$intial=0;
 	$nvpArray = array();
	while(strlen($nvpstr)){
		//postion of Key
		$keypos= strpos($nvpstr,'=');
		//position of value
		$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
		/*getting the Key and Value values and storing in a Associative Array*/
		$keyval=substr($nvpstr,$intial,$keypos);
		$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
		//decoding the respose
		$nvpArray[urldecode($keyval)] =urldecode( $valval);
		$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
     }
	return $nvpArray;
		} // End Deformat NVP
	
	function makeFile($name, $content)
	{
		$fh = fopen( $name, "w" );
		fwrite( $fh, $content);
		fclose( $fh );		
		return file_exists( $name ) ? 1 : 0;
	}
	
	function action_install( & $install )
	{
		$this->install =& $install;
	}
		
	function run()
	{
			switch( $this->install->mainClass->input['sub'] )
		{
			case 'main':
				$this->install_main();
			break;
			
			case 'admin':
				$this->admin();
			break;
						
			case 'license':
				$this->license();
			break;

			default:
				/* Output */
				$this->install->template->append( $this->install->template->install_page() );		
				$this->install->template->next_action = '?p=install&sub=main';
				$this->install->template->hide_next   = 1;
			break;	
		}
	}
function install_main()
	{
$output[] = 'Gathering configuration information..<br />';
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$filename = 'config.php';
		$core_conf = "<"."?php\n";
		$core_conf .='if(eregi(basename(__FILE__),$_SERVER[\'REQUEST_URI\'])) { die(\'Unauthorized Access Attempt\'); }
		//--
		// Config file settings
		//--
		//
		//note: ture = on, false = off.
		//
		$show = true; // users can view the files in a file viewer
		$del = ' . $this->install->saved_data['del'] . '; // users can delete files
		$edit = ' . $this->install->saved_data['edit'] . '; // users can edit files
		$rename = ' . $this->install->saved_data['rename'] . '; // users can rename files
		$copy = ' . $this->install->saved_data['copy'] . '; // users can copy files
		$move = ' . $this->install->saved_data['move'] . '; // users can move files
		$upload = ' . $this->install->saved_data['upload'] . '; // users can upload files
		
		//-- Admin settings --
		include(\'includes/admin.php\');

		//-- [END] --
		
		//---[DO NOT EDIT BEOND THIS POINT]---
		$debug = true;';

		$core_conf .= "\n".'?'.'>';
		$file = "$dir$filename";

		/* Write Configuration Files */
		$output[] = 'Writing configuration files...<br />';
		
		$this->makeFile( $file , $core_conf );
$output[] = 'Configuration file written...<br />';

		//-----------------------------------------
		// Next...
		//-----------------------------------------
		$this->install->template->append( $this->install->template->install_page_refresh( $output ) );
		$this->install->template->next_action = '?p=install&sub=admin';
		$this->install->template->hide_next   = 1;
	}

	function admin()
	{
	$output[] = 'Gathering administrative user information...<br />';
			$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$dir = $dir.'users/'.$this->install->saved_data[username].'.opuf';
	$password = crypt(md5($this->install->saved_data['pass_main']), md5($this->install->saved_data['username']));
$opuf ='  - User Password And Access Info -
Password="'.$password.'"
Power="Administrator"
 - User Settings -
Home="'.$this->install->saved_data['home_dir'].'"
trashdir="'.$this->install->saved_data['trash_dir'].'"
trashfile="'.$this->install->saved_data['trash_file'].'"
 - Login Information -
LoginAttempts="0"
LastLogin="00.00.00, 00:00:00"
IP="0.0.0.0"
----------------
oPanel User File';
		/* Write User File */
		$output[] = 'Creating Administrative user account...<br />';
	if(	$this->makeFile( $dir , $opuf ) ) {
	$output[] = 'Administrative user created...<br />'; }
		//-----------------------------------------
		// Next...
		//-----------------------------------------
		$this->install->template->append( $this->install->template->install_page_refresh( $output ) );
		$this->install->template->next_action = '?p=install&sub=license';
		$this->install->template->hide_next   = 1;
	}
	
	function license()
	{
	$output[] = 'Validating your license with the oPanel servers...<br />';
	$this->install->saved_data['license_error'] = false;
	$license = ''.$this->install->saved_data['lice_1'].'-'.$this->install->saved_data['lice_2'].'-'.$this->install->saved_data['lice_3'].'-'.$this->install->saved_data['lice_4'];
	/*$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://opanel.servehttp.com/premium/server.php');
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_POST, 1);
	$request="TYPE=".urlencode('LicenseCheck')."&CODE=".urlencode($license)."&DOMAIN=".urlencode($_SERVER["SERVER_NAME"]);
	curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
	$response = curl_exec($ch);
	$RepArray=$this->deformatNVP($response);
if(!isset($RepArray['LICETYPE']) && !isset($RepArray['ERROR'])) { $this->install->saved_data['license_error'] = true; }
			curl_close($ch);
		if(isset($RepArray['ERROR'])) { $this->install->saved_data['license_error'] = true; } */
	$output[] = 'Validation completed...<br />';
	if($this->install->saved_data['license_error'] == true) {
	$output[] = 'Skipping writing of license...<br />';
	} else {
	$output[] = 'Writing license to license file...<br />';
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( '\\'       , '/', $dir );
		$filename = 'license.oplf';
		$file = "$dir$filename";
		/* Write license File */
	$output[] = 'Writing oPanel license file...<br />';
	if(	$this->makeFile( $file , $license )) {
	$output[] = 'oPanel has been Licensed'; }
	}
		//-----------------------------------------
		// Next...
		//-----------------------------------------
		$this->install->template->append( $this->install->template->install_progress( $output ) );		
		$this->install->template->next_action = '?p=done';
	}
}
?>